//
//  CallNumberViewController.m
//  cloudCall
//
//  Created by zhang on 2021/10/29.
//

#import "CallNumberViewController.h"
#import <CloudCallBaseFrame/CommonFrame.h>
#import "LCCallNumberView.h"
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#import <CallWraper/CallWraper+Public.h>


@interface CallNumberViewController ()


@property (nonatomic,strong)LCCallNumberView *callView;

@end

@implementation CallNumberViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callingDidEnd) name:CallingDidEndNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callingDidError) name:CallingDidErrorNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callingDidConnect) name:CallingDidConnectNotification object:nil];
    [self setUpUI];
    if ([QTBClientCallCore shareCallCore].currentCall.status == QTBCallSessionStatusConnect) {
        self.callView.callingType = CONNECTING;
    } else {
        self.callView.callingType = AWAITING;
    }
}

-(void)setUpUI{
    self.callView = [[LCCallNumberView alloc] init];
    [self.view addSubview:self.callView];
    [self.callView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    INBWeakSelf;
    /// 缩小通话框
    self.callView.shrinkBlock = ^{
        INBStrongSelf;
        [self dismissViewControllerAnimated:YES completion:nil];
        if (self.shrinkBlock) {
            self.shrinkBlock();
        }
    };
    /// 开/关麦克风
    self.callView.voiceBlock = ^(BOOL open) {
        INBStrongSelf;
        [self openMicrophone:open];
    };
    ///免提
    self.callView.handsfreeBlock = ^(BOOL open) {
        INBStrongSelf;
        [self opentHandsfree:open];
    };
    /// 挂断
    self.callView.hangupBlock = ^{
        [[QTBClientCallCore shareCallCore] sendOnHook];
    };
    self.callView.answerBlock = ^{
        [[QTBClientCallCore shareCallCore] answerCall];
    };
}


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self openMicrophone:YES];
    [self opentHandsfree:NO];
    [[CallWraperOc GetInstance] SetInputMute:YES];
    [self configureAVAudioSession:AVAudioSessionPortOverrideNone];
}

/// 开/关麦克风
- (void)openMicrophone:(BOOL)open {
    if (open) {
        [[CallWraperOc GetInstance] SetInputMute:YES];
    }else {
        [[CallWraperOc GetInstance] SetInputMute:NO];
    }
}

//免提
- (void)opentHandsfree:(BOOL)open {
    if (open) {
        [self configureAVAudioSession:AVAudioSessionPortOverrideSpeaker];
    }else {
        [self configureAVAudioSession:AVAudioSessionPortOverrideNone];
    }
}

- (void) configureAVAudioSession:(AVAudioSessionPortOverride)audioSessionPortOverride{
    AVAudioSession* session = [AVAudioSession sharedInstance];
    BOOL success;
    NSError* error;
    success = [session setCategory:AVAudioSessionCategoryPlayAndRecord
                             error:&error];//
    if (!success)  NSLog(@"AVAudioSession error setting category:%@",error);
    success = [session overrideOutputAudioPort:audioSessionPortOverride
                                         error:&error];
    if (!success)  NSLog(@"AVAudioSession error overrideOutputAudioPort:%@",error);
    success = [session setActive:YES error:&error];
    if (!success) NSLog(@"AVAudioSession error activating: %@",error);
    else NSLog(@"audioSession active");
}



-(void)callingDidEnd{
    self.callView.callingType = HANGUP;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self dismissViewControllerAnimated:YES completion:nil];
    });
}


-(void)callingDidError{
//    self.callView.callingType = HANGUP;
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [self dismissViewControllerAnimated:YES completion:nil];
//    });
    [self callingDidEnd];
}


-(void)callingDidConnect{
    self.callView.callingType = CONNECTING;
}


-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}



@end
